<?php

class index extends cms_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        //$this->load->model('imp_model');
    }

    public function index() {
        $this->load->model('trades_model');
        $this->pass['trades'] = $this->trades_model->get(array('homepage' => 'YES'));
        $this->pass['slider'] = get_slider();
        $this->themes->render('index/default', $this->pass);
    }

    function setlang() {
        if (LANG == 'ar')
            $this->session->set_userdata('def_lang', 'en');
        else
            $this->session->set_userdata('def_lang', 'ar');

        redirect($this->agent->referrer());
    }

    function mailinglist($code = FALSE) {
        $this->load->model('maillinglist_model', 'mlist');
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->load->library('form_validation');
            $this->form_validation->set_rules('email', lang('label_email'), 'trim|required|is_unique[mailinglist.email]|valid_email');
            if ($this->form_validation->run()) {
                $code = md5($this->input->post('email') + time());
                $save['email'] = $this->input->post('email');
                $save['code'] = $code;
                if ($this->mlist->save($save)) {
                    $to = $this->input->post('email');
                    $subject = lang('message_subscribe_subject');
                    $msg = str_replace('%s', $code, lang('message_subscribe_body'));
                    $headers = 'From: ' . get_option('site_email');
                    $this->load->library('email_system');
                    $this->email_system->SendEmail($to, $subject, false, false, false, $msg);
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('type' => 'success', 'url' => '', 'message' => 'message_mailing_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } elseif ($code) {
            $mail = $this->mlist->is_found(array('code' => $code), array(), 'row', '');
            $save['active'] = 'YES';
            $save['code'] = '';
            if ($this->mlist->save($save, array('id' => $mail->id), TRUE)) {
                $this->themes->set_message(array('type' => 'success', 'url' => '', 'message' => 'message_activate_list_success'));
            }
        } else {
            show_404();
        }
    }

    function stores() {
        $this->load->model('store_model', 'stores');
        $return = $this->stores->get_stores(array('active' => 'YES'));
        $this->pass = $return;
        if (count($this->stores->get(array('owner_id' => $this->user_id))) > 0) {
            $this->pass['createnow'] = false;
        } else {
            $this->pass['createnow'] = true;
        }
        $this->themes->render('index/stores', $this->pass);
    }

    function checked() {
        if ($this->session->userdata('saved')) {
            $this->session->unset_userdata('saved');
            $this->themes->render('cart/checked');
        } else {
            redirect('cart');
        }
    }

    function getads($height = false, $size = false) {
        if ($height && $size) {
            $count = ($height - $size) / 260;
            $count = explode(".", $count);
            if (isset($count[1])) {
                if (substr((int) $count[1], 0, 1) <= 3)
                    $count = $count[0];
                else
                    $count = $count[0] + 1;
            }else {
                $count = $count[0];
            }
            $topads = get_random_ads($count);
            $return = "";
            $le = 0;
            $ri = 0;
            $left = "";
            $right = "";
            $click = false;
            for ($i = 0; $i < $count; $i++) {
                if (isset($topads[$i])) {
                    if($topads[$i]->max_click != "0"){
                        $click = true;
                        if($topads[$i]->click >= $topads[$i]->max_click)
                            continue;
                    }elseif($topads[$i]->max_view != "0"){
                        if($topads[$i]->view >= $topads[$i]->max_view)
                            continue;
                    }
                    $left .= "<a href='";
                    if (isset($topads[$i]->url)) {
                        $left .= $topads[$i]->url;
                    } else {
                        $left .= base_url('pages/view/adv');
                    }
                    $left .= "' class='ad";
                    if($click){
                        $left .= " clickad' data-ad='".$topads[$i]->id."'";
                        $click = false;
                    }else{
                        $left .= "'";
                    }
                    $left .= " target='_blank'><img src='";
                    if (isset($topads[$i]->image))
                        $left .= base_url(ADS_PATH . $topads[$i]->image);
                    else
                        $left .= base_url(ADS_PATH . 'default.png');
                    $left .= "' style='width: 210px;height:260px' /></a>";
                    inc_adv_view($topads[$i]->id);
                }else {
                    $left .= "<a href='";
                    $left .= base_url('pages/view/adv');
                    $left .= "' class='ad' target='_blank'><img src='";
                    $left .= base_url(ADS_PATH . 'default.png');
                    $left .= "' /></a>";
                }
                $le++;
            }
            if ($le < $count) {
                for ($i = $le; $i < $count; $i++) {
                    $left .= "<a href='";
                    $left .= base_url('pages/view/adv');
                    $left .= "' class='ad' target='_blank'><img src='";
                    $left .= base_url(ADS_PATH . 'default.png');
                    $left .= "' style='width: 210px;height:260px' /></a>";
                }
            }

            $return['left'] = $left;
            $return = json_encode($return);
            die($return);
        } else {
            die();
        }
    }

    function payment_method() {
        $this->load->model('banks_model', 'banks');
        $this->pass['banks'] = $this->banks->get();
        $this->themes->render('index/payment_method', $this->pass);
    }

    function shiping_method() {
        $this->pass['companies'] = get_shiping_com();
        $this->themes->render('index/shiping_method', $this->pass);
    }

    function packages() {
        $this->load->model('packages_details_model', 'pdetails');
        $this->load->model('packages_model', 'packages');
        $this->pass['packages'] = $this->packages->get();
        $this->pass['pdetails'] = $this->pdetails->get();
        $this->themes->render('index/packages', $this->pass);
    }

    function contact() {
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('captcha', lang('label_captcha'), 'trim|required|callback_match_captcha');
            $this->form_validation->set_rules('email', lang('label_email'), 'trim|required|valid_email');
            $this->form_validation->set_rules('issue', lang('label_issue_type'), 'trim|required');
            $this->form_validation->set_rules('message', lang('label_message'), 'trim|required');

            if ($this->form_validation->run()) {
                $to = get_option('site_email');
                $subject = str_replace('%s', $this->input->post('email'), lang('message_contact_us_from'));
                $msg = str_replace(
                        array('%from%', '%issue%', '%message%'), array($this->input->post('email'), lang('lable_contact_' . $this->input->post('issue')), $this->input->post('message')), lang('message_contact_body'));
                $this->load->library('email_system');
                $this->email_system->SendEmail($to, $subject, false, false, false, $msg);
                $this->themes->set_message(array('type' => 'success', 'url' => '', 'message' => 'message_contact_send_success'));
            } else {
                get_validation_errors();
            }
        }

        $this->themes->render('index/contact', $this->pass, false, 'wide.php');
    }

    function match_captcha($str) {
        if (strtolower($str) == $this->session->userdata('vercode')) {
            $this->session->unset_userdata('vercode');
            return TRUE;
        } else {
            $this->session->unset_userdata('vercode');
            $this->form_validation->set_message('match_captcha', lang('message_wrong_captcha'));
            return FALSE;
        }
    }

    function captcha() {
        session_start();
        $text = strtoupper(random_string('alnum', 5));
        $this->session->set_userdata('vercode', strtolower($text));
        $height = 25;
        $width = 65;
        $image_p = imagecreate($width, $height);
        $black = imagecolorallocate($image_p, 255, 255, 255);
        $white = imagecolorallocate($image_p, 21, 124, 150);

        $font_size = 14;
        imagestring($image_p, $font_size, 5, 5, $text, $white);
        imagepng($image_p, null, 0);
    }

}

